﻿@model IEnumerable<WarOfDev.Web.Models.MessageModel>
<div class="container_12 grid_6 message-list-buttons">
    <ul>
        <li>Top</li>
        <li>-</li>
        <li>All</li>
        <li>-</li>
        <li>Following</li>
    </ul>
</div>
<div class="container_12 grid_6"  id="test">
    @foreach (var item in Model)
    {
        <div class="message-list-info">
            <div class="container_12 grid_1 omega">
                <div class="user-photo level-head level-1">
                </div>
            </div>

            <div class="container_12 grid_5 alpha">
                <div>
                    <ul>
                        <li><a>@item.Message.Developer.NickName</a></li>
                        
                    </ul>
                </div>
                <div>
                    @Html.DisplayFor(modelItem => item.Message.MessageContent)

                    @foreach (var MessageFile in item.Message.Files)
                    {
                       //<img src = "/Content/Files/testFiles/@MessageFile.FileName" alt=""/>
                        <img src = "/Content/Files/@item.Message.Developer.TrueName/@MessageFile.FileName" alt=""/>
                        //@Html.ActionLink(MessageFile.FileName, "GetFileFromDisk", new WarOfDev.EF.MessageFile() { Id = MessageFile.Id })
                    }

                    @foreach (var link in item.Message.Links)
                    { 
                        <a href="@link.LinkHref" target="@link.Target">@link.LinkText </a>
                    }
                </div>
                <div>
                    <a href="javascript:void(0)" class="aLike" data="@item.Message.Id">
                        @(item.Message.Operations.Count(o => o.OperateType == (int)WarOfDev.Core.Common.EnumMessageOprateType.Like) > 0 ? "UnLike" : "Like")
                    </a><a href="javascript:void(0)" class="aShare">Share</a> <a href="javascript:void(0)"
                        class="aReply">Reply</a>
                </div>

                <div class="replyList">
                @if (item.ReplyList.Count > 0)
                {
                    foreach (var reply in item.ReplyList)
                    {
                        <div><a>@reply.Developer.NickName</a> : @reply.MessageContent</div>
                    }
                }
                </div>
                    

                <div class="treply" data="@item.Message.Id" style="padding:2px; border:1px solid #ccc;   color:#999;" >
                    Write a reply...
                </div>
                
            </div>
            <div class="clear">
            tete
            </div>
        </div>
        <div class="clear">
        </div>
       
    }
</div>

<script type="text/javascript">

    $(".aLike").live("click", function () {
        var msgId = $(this).attr("data");
        var href = this;

        var action=$(this).text().trim()=="UnLike"? "@(Url.Action("UnLike", "Message"))" : "@(Url.Action("Like", "Message"))" ;
        var actionText= $(this).text().trim()=="Like"?"UnLike":"Like";
        
        $.ajax({
            url:  action,
            type: "Post",
            data: { id: msgId },
            success: function (e) {
                if (e.success) {
                    $(href).text(actionText);
                }
            },
        });

    });


    function ClickToTxtBox(){
        var clickFn=function(){
            var textarea= document.createElement("textarea");
            var btnReply= document.createElement("input");
            var msgId=$(this).attr("data");
            var replyAction="@(Url.Action("Reply", "Message"))";
            var container=this;
            var appendHtml="";

            $(btnReply).attr({"type":"button","disabled":"true"}).val("Reply").css({float:"right","margin-top":"10px"});
            //$(btnReply).disabled=false;
            $(textarea).css({height:"100%",width:"100%",border:"none"}).attr({"overflow":"hidden","rows":"2"});

            $(this).html(textarea);
            $(this).append(btnReply);
            $(this).unbind("click");
            textarea.focus();

            $(textarea).bind("keypress",function(e){
                
                var height=parseInt($(this).css("height"));
                var rows=$(this).attr("rows");
                    
                 if(e.keyCode==13){
                     $(this).css("height",height+15+"px");
                 }

            }).bind("keyup",function(){

                if($(this).val().trim().length>0){
                    $(btnReply).removeAttr("disabled");
                }
                else
                {
                 $(btnReply).attr("disabled", true);
                }

            }).bind("blur",function(){
                 
                 if($(textarea).val().trim().length==0)
                 {
                     $(container).html("write a reply...");
                     $(container).bind("click",clickFn);
                 }
            });

            $(btnReply).click(function(){
                        
                $.ajax({
                    url: replyAction,
                    type: "post",
                    data: {id: msgId,message:$(textarea).text() },
                    success:function(e){
                        if(e.success){
                            appendHtml="<div>"
                            appendHtml+="<a>"+ e.developer +"</a> "+": " + $(textarea).text();
                            appendHtml+="</div>";

                            $(container).parent().find(".replyList").prepend(appendHtml);
                            $(container).html("write a reply...");
                            $(container).bind("click",clickFn);

                        }
                    }
                });
            });
        }

        return {
            init:function(className){
                $("."+className).bind("click", clickFn);
            },
            reBind:function(obj){
                $(obj).click(clickFn);
            }
        };
    }
    
    
    var clickToBoxHelper= new ClickToTxtBox();
    clickToBoxHelper.init("treply");

    $(".aShare").live("click", function () { });
    $(".aReply").live("click",function(){
        $(this).parent().next().next().click();
    });
</script>
